﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.ScrumTool.Service.Interface;
using UIT.ScrumTool.Service.Model;
using UIT.ScrumTool.DTO;

namespace UIT.ScrumTool.Service.Business
{
    public partial class ControlService : IProjectUserService
    {
        public List<ProjectUserDto> ProjectUserGetByAll()
        {
            List<ProjectUserDto> lsprojectUserDto = new List<ProjectUserDto>();
            try
            {
                List<ProjectUser> ls = dataContext.sp_ProjectUsers_GetByAll().ToList();
                foreach (var projectUser in ls)
                {
                    lsprojectUserDto.Add(ProjectUserConvertProjectUserDto(projectUser));
                }
                return lsprojectUserDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ProjectUserDto ProjectUserGetById(int id)
        {
            try
            {
                return ProjectUserConvertProjectUserDto(dataContext.sp_ProjectUsers_GetById(id).Single());
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ProjectUserDto> ProjectUserGetByProjectId(int projectId)
        {
            List<ProjectUserDto> lsprojectUserDto = new List<ProjectUserDto>();
            try
            {
                List<ProjectUser> ls = dataContext.sp_ProjectUsers_GetByProjectId(projectId).ToList();
                foreach (var projectUser in ls)
                {
                    lsprojectUserDto.Add(ProjectUserConvertProjectUserDto(projectUser));
                }
                return lsprojectUserDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ProjectUserDto> ProjectUserGetByProjectIdNoTeam(int projectId)
        {
            List<ProjectUserDto> lsprojectUserDto = new List<ProjectUserDto>();
            try
            {
                List<ProjectUser> ls = dataContext.sp_ProjectUsers_GetByProjectIdNoTeam(projectId).ToList();
                foreach (var projectUser in ls)
                {
                    lsprojectUserDto.Add(ProjectUserConvertProjectUserDto(projectUser));
                }
                return lsprojectUserDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ProjectUserDto> ProjectUserGetByTeamId(int teamId)
        {
            List<ProjectUserDto> lsprojectUserDto = new List<ProjectUserDto>();
            try
            {
                List<ProjectUser> ls = dataContext.sp_ProjectUsers_GetByTeamId(teamId).ToList();
                foreach (var projectUser in ls)
                {
                    lsprojectUserDto.Add(ProjectUserConvertProjectUserDto(projectUser));
                }
                return lsprojectUserDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public int ProjectUserInsert(ProjectUserDto projectUserDto)
        {
            ProjectUser projectUser = new ProjectUser();
            projectUser.UserId = projectUserDto.UserId;
            projectUser.ProjectId = projectUserDto.ProjectId;
            projectUser.UserRole = projectUserDto.UserRole;
            projectUser.TeamId = projectUserDto.TeamId;

            try
            {
                dataContext.ProjectUsers.InsertOnSubmit(projectUser);
                dataContext.SubmitChanges();
                return projectUser.Id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public bool ProjectUserUpdate(ProjectUserDto projectUser)
        {
            try
            {
                dataContext.sp_ProjectUsers_Update(projectUser.Id, projectUser.ProjectId, projectUser.UserId, projectUser.UserRole, projectUser.TeamId);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool ProjectUserUpdateRole(int id, int userRole)
        {
            try
            {
                dataContext.sp_ProjectUsers_UpdateRole(id, userRole);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool ProjectUserDelete(int id)
        {
            try
            {
                dataContext.sp_ProjectUsers_Delete(id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool ProjectUserDeleteByProjectId(int projectId)
        {
            try
            {
                dataContext.sp_ProjectUser_DeleteByProjectId(projectId);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static ProjectUserDto ProjectUserConvertProjectUserDto(ProjectUser projectUser)
        {
            ProjectUserDto projectUserDto = new ProjectUserDto();
            projectUserDto.Id = projectUser.Id;
            projectUserDto.ProjectId = (int) projectUser.ProjectId;
            projectUserDto.UserId = (int)projectUser.UserId;
            projectUserDto.UserRole = (int)projectUser.UserRole;
            projectUserDto.TeamId = projectUser.TeamId == null ? 0 : (int) projectUser.TeamId;
            return projectUserDto;
        }
    }
}
